<template>
  <view>
    <!-- 这是搜索框 -->
    <view class="search-box">
      <uni-search-bar @input="input" :radius="100" :focus="true" :bgColor="'#FFF'"
        cancel-button="none"></uni-search-bar>
    </view>

    <!-- 这是搜索建议列表 -->
    <view class="sugg-list" v-if="searchResults.length !== 0">
      <view class="sugg-item" v-for="(item,i) in searchResults" :key="i" @click="gotoDetail(item)">
        <view class="goods-name">{{item.goods_name}}</view>
        <uni-icons type="arrowright" size="16"></uni-icons>
      </view>
    </view>

    <!-- 搜索历史 -->
    <view class="history-box" v-else>
      <!-- 标题区域 -->
      <view class="history-title">
        <text>搜索历史</text>
        <uni-icons type="trash" size="17" @click="clean"></uni-icons>
      </view>
      <!-- 列表区域 -->
      <view class="history-list">
        <uni-tag v-for="(item,i) in historys" :key="i" :text="item" @click="gotoGoodsList(item)"></uni-tag>
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {

        timer: null,
        //搜索关键词
        kw: '',
        //搜索结果列表
        searchResults: [],
        //搜索关键词的历史记录
        historyList: []
 
      };
    },
    
    onLoad() {
      this.historyList = JSON.parse(uni.getStorageSync('kw') || '[]')
    },
    
    methods: {
      // input 输入事件的内容
      input(e) {
        // console.log(e);
        clearTimeout(this.timer)

        this.timer = setTimeout(() => {
          this.kw = e
          console.log(this.kw)
          //根据关键词,查询搜索建议列表
          this.getSearchList()

        }, 500)
      },
 
      gotoDetail(item) {
        console.log(item.goods_id);
        uni.navigateTo({
          url: '/subpkg/goods_detail/goods_detail?goods_id=' + item.goods_id
        })
      },

      async getSearchList() {
        //判断关键词是否为空
        if (this.kw === '') {
          this.searchResults = []
          return
        }
        //发起请求,获取搜索建议列表
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/goods/qsearch', {
          query: this.kw
        })
        if (res.meta.status !== 200) return uni.$showMsg()
        this.searchResults = res.message
        
        this.saveSearchHistory()
        
      },
      // 保存搜索历史记录
      saveSearchHistory() {
         // this.historyList.push(this.kw)
         
         //1.将 Array 数组转化为 set 对象
         const set = new Set(this.historyList)
         //2.调用 Set 对象的 delete 方法,删除对应的元素
         set.delete(this.kw)
         //3.调用 Set 对象的 add 方法,向 Set 中添加元素
         set.add(this.kw) 
         //4.将 Set 对象转化为 Array 数组
         this.historyList = Array.from(set)
         //调用 uni.setStorageSync (key,value) 将搜索历史记录持久化存储在本地
         uni.setStorageSync('kw',JSON.stringify(this.historyList))
      },
      
      clean() {
        this.historyList = []
        uni.setStorageSync('kw','[]')
      },
      gotoGoodsList(kw) {
        uni.navigateTo({
          url:'/subpkg/goods_list/goods_list?query=' + kw
        })
      }


    },
    
    //定义计算对象
    computed: {
      //定义计算属性
      historys () {
        return [...this.historyList].reverse()
      }
    }
    
  }
</script>

<style lang="scss">
  .search-box {
    position: sticky;
    top: 0;
    z-index: 999;
  }

  .sugg-list {
    padding: 0 5px;

    .sugg-item {
      // height: 50px;
      display: flex;
      justify-content: space-between;
      align-items: center;
      font-size: 12px;
      padding: 13px 0px;
      border-bottom: 1px solid #e0e0e0;

      .goods-name {
        //文字不允许换行 (单行文字)
        white-space: nowrap;
        //溢出部分隐藏
        overflow: hidden;
        //文字溢出后使用 ...代替
        text-overflow: ellipsis;
        margin-right: 3px;
      }
    }
  }

  .history-box {
    padding: 0px 5px;

    .history-title {
      display: flex;
      justify-content: space-between;
      height: 40px;
      align-items: center;
      font-size: 13px;
      border-bottom: 0.5px soild #EFEFEF;
    }

    .history-list {
      display: flex;
      flex-wrap: wrap;

      .uni-tag {
        margin-top: 5px;
        margin-right: 5px;
        background-color: #f9f9f9;
        color: #333333;
        font-size: 13px;
      }

    }

  }
</style>